package com.turman.blibrary.log;

import android.graphics.Color;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.turman.blibrary.util.BDisplayUtil;


public class BLogViewProvider {
    public static final String TAG_FLOATING_VIEW = "TAG_FLOATING_VIEW";
    private static final String TAG_LOG_VIEW = "TAG_LOG_VIEW";

    private FrameLayout rootView;
    private RecyclerView recyclerView;
    private boolean isOpen;
    private FrameLayout logView;
    private TextView floatingView;

    public BLogViewProvider(FrameLayout rootView, RecyclerView recyclerView) {
        this.rootView = rootView;
        this.recyclerView = recyclerView;
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void showFloatingView() {
        if (rootView.findViewWithTag(TAG_FLOATING_VIEW) != null) {
            return;
        }
        if (logView!=null) {
            logView.removeView(recyclerView);
            rootView.removeView(logView);
        }

        floatingView = new TextView(rootView.getContext());
        floatingView.setBackgroundColor(Color.BLACK);
        floatingView.setTextColor(Color.WHITE);
        floatingView.setText("ShowLog");
//        floatingView.setTextSize(12, TypedValue.COMPLEX_UNIT_SP);
        floatingView.setTag(TAG_FLOATING_VIEW);
        floatingView.setOnClickListener((v)-> showLogView());

        FrameLayout.LayoutParams params =
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
        rootView.addView(floatingView, params);

        isOpen = false;
    }

    public void showLogView() {
        if (rootView.findViewWithTag(TAG_LOG_VIEW) != null) {
            return;
        }
        if (floatingView!=null) {
            rootView.removeView(floatingView);
        }

        logView = new FrameLayout(rootView.getContext());
        logView.setTag(TAG_LOG_VIEW);
        logView.setBackgroundColor(Color.BLACK);
        FrameLayout.LayoutParams listParams =
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        logView.addView(recyclerView, listParams);
        TextView closeView = new TextView(rootView.getContext());
        closeView.setBackgroundColor(Color.WHITE);
        closeView.setTextColor(Color.BLACK);
        closeView.setText("[ x ]");
//        closeView.setTextSize(12, TypedValue.COMPLEX_UNIT_SP);
        closeView.setOnClickListener((v) -> {
            showFloatingView();
        });
        FrameLayout.LayoutParams closeParams =
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        closeParams.gravity = Gravity.RIGHT | Gravity.TOP;
        closeParams.setMargins(5, 5, 5, 5);
        logView.addView(closeView, closeParams);

        FrameLayout.LayoutParams params =
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, BDisplayUtil.dp2px(180,rootView.getResources()));
        params.gravity = Gravity.BOTTOM;
        rootView.addView(logView, params);

        isOpen = true;
    }
}


















